package com.example.c94s3plyhyfc.web;

import com.example.c94s3plyhyfc.biz.UserBiz;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginIntercepter implements HandlerInterceptor {

    @Resource
    UserBiz ub;

    /**
     * 前置拦截, 即: 在请求到达控制器或目标资源之前进行拦截
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //控制器: com.example.c94s3plyhyfc.web.BbsController#save(Bbs, Errors)
        System.out.println("控制器: " + handler);
        if (request.getSession().getAttribute("loginedUser") == null) {
            response.sendRedirect("/login.html");
            return false;
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);  // true
    }
}
